package com.example.myapplication22;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication22.utils.MyHelper;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Update extends AppCompatActivity {
TextView date;
EditText nr,title;
Button btn_bc;
    String _id;
    String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        date = findViewById(R.id.time1);
        nr=findViewById(R.id.nr);
        title=findViewById(R.id.tit);
        btn_bc=findViewById(R.id.btn_bc);

        //设置时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");

         date.setText(simpleDateFormat.format(System.currentTimeMillis()+28800000));

        final Bundle bundle = getIntent().getExtras();
        _id = bundle.getString("_id");
        id = bundle.getString("id");
        //显示数据
        SQLiteDatabase w = new MyHelper(Update.this).getWritableDatabase();
        Cursor cursor = w.rawQuery("select * from info where _id=?", new String[]{_id});
        if (cursor.getCount()>0){
            while (cursor.moveToNext()){
                title.setText(cursor.getString(2));
                nr.setText(cursor.getString(3));
            }
        }
        w.close();
        //启动线程
        new Thread(new AutoUpdate()).start();
        //修改数据
       // sad
        btn_bc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyHelper h = new MyHelper(Update.this);
              boolean b=  h.getUpdate(title.getText().toString(),nr.getText().toString(),date.getText().toString(),_id);
              if (b){
                  System.out.println("success");
                  Toast.makeText(Update.this,"修改成功",Toast.LENGTH_LONG).show();
                  Intent intent = new Intent(Update.this, List.class);
                  Bundle bundle1 = new Bundle();
                  bundle1.putString("id",id);
                  intent.putExtras(bundle1);
                  startActivity(intent);
              }else {
                  System.out.println("error");
              }
            }
        });

    }
    //修改内容自动保存
    class AutoUpdate implements Runnable{
        @Override
        public void run() {
            MyHelper myHelper = new MyHelper(Update.this);
            while (true){
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                boolean f = myHelper.getUpdate(title.getText().toString(), nr.getText().toString(), date.getText().toString(), _id);
                if (f){
                    Looper.prepare();
                    Toast.makeText(Update.this,"自动保存成功",Toast.LENGTH_LONG).show();
                    Looper.loop();
                    System.out.println("修改-自动保存成功");
                }else {
                    System.out.println("error");
                }


            }
        }
    }

}
